import { InitModalMethod, ParseModalMethod } from './defination';

/**
 * 设置表单属性
 * @param { Object } form 表单数据
 * @param { Object } workstep cached的ai工作流
 */
export const initModalData = (form = {}, workstep = {}) => {
	if (!form || !workstep || !workstep.do) {
		return;
	}

	form.waitTime = workstep.do.data?.speechBeginWaitingTimeMS; // 1
	// typeOfCollectedResults
	form.typeOfCollectedResults = workstep.typeOfCollectedResults;
	// yes
	form.nextStepYes = workstep.answer[0]?.do?.id;
	// other
	form.otherTimesMax = workstep.errorRemind?.maximum; // 2
	form.nextStepOther = workstep.errorRemind?.do?.id;
	// 关联变量数据
	form.associatedVariable = workstep.associatedVariable;

	/** 变量存储处理开始 */
	form['wordsYesVariableId'] = workstep.answer[0].do.variableId;
	form['wordsYesVariables'] = workstep.answer[0].do.variables?.filter(item => form['wordsYes']?.indexOf(`{{${item.name}}}`) !== -1);
	/** 变量存储处理结束 */

	form.name = workstep.name;
};
InitModalMethod['soundReception'] = initModalData;

/**
 * 解析表单属性
 * @param { Object } form 表单数据
 * @param { Object } workstep cached的ai工作流
 */
export const parseModalData = (form = {}, workstep = {}) => {
	console.log(form, workstep);
	if (!form || !workstep || !workstep.do) {
		return;
	}
	workstep.do.data.speechBeginWaitingTimeMS = form.waitTime; // 1
	// typeOfCollectedResults
	workstep.typeOfCollectedResults = form.typeOfCollectedResults;
	// yes
	workstep.answer[0].do.id = form.nextStepYes;
	workstep.answer[0].do.no = form.nextStepYes;

	// other
	workstep.errorRemind.do.id = form.nextStepOther;
	workstep.errorRemind.do.no = form.nextStepOther;
	workstep.errorRemind.maximum = form.otherTimesMax; // 4

	// 关联变量数据
	workstep.associatedVariable = form.associatedVariable;

	/** 变量存储处理开始 */
	workstep.do.variableId = form['aiSpeakVariableId'];
	workstep.do.variables = form['aiSpeakVariables']?.filter(item => form['aiSpeak'].indexOf(`{{${item.name}}}`) !== -1);
	/** 变量存储处理结束 */

	workstep.name = form.name;
};
ParseModalMethod['soundReception'] = parseModalData;
// {
//     "id": "soundReception_82",
//     "type": "soundReception",
//     "robotProperty": {
//         "needHearing": true,
//         "videoType": "preorder",
//         "audioType": "preorder"
//     },
//     "do": {
//         "command": "listen",
//         "data": {
//             "judgmentType": "non-empty",
//             "speechBeginWaitingTimeMS": 1
//         }
//     },
//     "answer": [
//         {
//             "key": [
//                 "not-empty"
//             ],
//             "do": {
//                 "command": "jump-to",
//                 "id": "BlankNode_83",
//                 "no": "BlankNode_83"
//             },
//             "type": "ok"
//         },
//         {
//             "key": "others",
//             "do": {
//                 "command": "listen",
//                 "data": {
//                     "judgmentType": "non-empty",
//                     "speechBeginWaitingTimeMS": "3000"
//                 }
//             }
//         }
//     ],
//     "errorRemind": {
//         "do": {
//             "command": "jump-to",
//             "id": "BlankNode_84",
//             "no": "BlankNode_84"
//         },
//         "type": "no",
//         "maximum": 1
//     },
//     "typeOfCollectedResults": "numeric",
//     "associatedVariable": {
//         "id": 305,
//         "name": "age",
//         "describe": "年龄",
//         "initMode": "user-data",
//         "type": "numeric",
//         "url": "",
//         "method": "",
//         "params": [],
//         "dataMasking": {}
//     },
//     "no": "soundReception_82",
//     "name": "",
//     "completed": true,
//     "hasStartNode": false
// }
